<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.name" placeholder="名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="getMenus">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <el-table :data="list" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="name" label="菜单名称"  width="220" sortable>
            </el-table-column>
            <el-table-column prop="path" label="路径" width="180" >
            </el-table-column>
            <el-table-column prop="component" label="component" width="320" >
            </el-table-column>
            <el-table-column prop="actionCode" label="权限code" width="180" sortable>
            </el-table-column>
            <el-table-column prop="createTimeStr" label="添加时间" width="240" sortable>
            </el-table-column>
            <el-table-column label="操作" width="320">
                <template scope="scope">
                    <el-button type="success" size="small" @click="handleAdd(scope.$index, scope.row)">添加子菜单</el-button>
                    <el-button type="primary" size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--新增界面-->
        <el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="名称" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="路径" prop="path" >
                    <el-input type="tel" v-model="addForm.path"  auto-complete="off" ></el-input>
                </el-form-item>
                <el-form-item label="component" prop="component" >
                    <el-input v-model="addForm.component"  auto-complete="off" ></el-input>
                </el-form-item>
                <el-form-item label="所属菜单" prop="parentId" >
                    <template>
                        <el-select v-model="addForm.parentId" filterable placeholder="请选择父级菜单">
                            <el-option
                                    v-for="item in options"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id">
                            </el-option>
                        </el-select>
                    </template>
                </el-form-item>
                <el-form-item label="图表" prop="iconCls" >
                    <el-input v-model="addForm.iconCls"  auto-complete="off" ></el-input>
                </el-form-item>
                <el-form-item label="Action Code" prop="actionCode" >
                    <el-input v-model="addForm.actionCode"  auto-complete="off" ></el-input>
                </el-form-item>
                <el-form-item label="隐私" prop="hidden" >
                    <template>
                        <el-radio v-model="addForm.hidden" label="1">是</el-radio>
                        <el-radio v-model="addForm.hidden" label="0">否</el-radio>
                    </template>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>

        <!--新增界面-->
        <el-dialog title="新增" v-model="editFormVisible" :close-on-click-modal="false">
            <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
                <el-form-item label="名称" prop="name">
                    <el-input v-model="editForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="路径" prop="path" >
                    <el-input type="tel" v-model="editForm.path"  auto-complete="off" ></el-input>
                </el-form-item>
                <el-form-item label="component" prop="component" >
                    <el-input v-model="editForm.component"  auto-complete="off" ></el-input>
                </el-form-item>
                <el-form-item label="所属菜单" prop="parentId" >
                    <template>
                        <el-select v-model="editForm.parentId" filterable placeholder="请选择父级菜单">
                            <el-option
                                    v-for="item in options"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id">
                            </el-option>
                        </el-select>
                    </template>
                </el-form-item>
                <el-form-item label="图表" prop="iconCls" >
                    <el-input v-model="editForm.iconCls"  auto-complete="off" ></el-input>
                </el-form-item>
                <el-form-item label="Action Code" prop="actionCode" >
                    <el-input v-model="editForm.actionCode"  auto-complete="off" ></el-input>
                </el-form-item>
                <el-form-item label="隐私" prop="hidden" >
                    <template>
                        <el-radio v-model="addForm.hidden" label="1">是</el-radio>
                        <el-radio v-model="addForm.hidden" label="0">否</el-radio>
                    </template>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="editFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>


    </section>
</template>

<script>
    import {getMenuList , saveMenu,deleteMenuApi} from '../../api/api'
    export default {
        data() {
            return {
                filters: {
                    name: ''
                },
                list: [],
                options:[],
                listLoading: false,
                sels: [],
                addFormVisible: false,
                editFormVisible: false,
                addLoading: false,
                editLoading: false,
                addForm :{
                    name: '',
                    parentId:'',
                    path: '',
                    component: '',
                    iconCls: '',
                    actionCode: '',
                    hidden:'0'
                },
                editForm :{
                    name: '',
                    parentId:'',
                    path: '',
                    component: '',
                    iconCls: '',
                    actionCode: '',
                    hidden:'0'
                },
                addFormRules: {
                    name: [
                        { required: true, message: '请输入菜单名称', trigger: 'blur' }
                    ],
                    path: [
                        { required: true, message: '请输入访问路径', trigger: 'blur' }
                    ],
                    component:[
                        { required: true, message: '请输入Component', trigger: 'blur' }
                    ],
                    actionCode: [
                        { required: true, message: '请输入ActionCode', trigger: 'blur' }
                    ]
                },
                editFormRules: {
                    name: [
                        { required: true, message: '请输入菜单名称', trigger: 'blur' }
                    ],
                    path: [
                        { required: true, message: '请输入访问路径', trigger: 'blur' }
                    ],
                    component:[
                        { required: true, message: '请输入Component', trigger: 'blur' }
                    ],
                    actionCode: [
                        { required: true, message: '请输入ActionCode', trigger: 'blur' }
                    ]
                },
                //删除
                handleDel: function (index, row) {
                    this.$confirm('确认删除该记录吗?', '提示', {
                        type: 'warning'
                    }).then(() => {
                        this.listLoading = true;
                        //NProgress.start();
                        let para = { id: row.id };
                        console.log(para)
                        deleteMenuApi(para).then((res) => {
                            this.listLoading = false;
                            //NProgress.done();
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.getMenus();
                        });
                    }).catch((e) => {
                        this.listLoading = false;
                        this.$message({
                            message: e,
                            type: 'error'
                        });
                    });
                }
            }
        },
        methods: {
            getMenus: function () {
                let para = {
                    name: this.filters.name
                };
                this.listLoading = true;
                //NProgress.start();
                getMenuList(para).then((res) => {
                    let data = res.data.data;
                    this.list = data.list;
                    this.options = data.options;
                    this.listLoading = false;
                    //NProgress.done();
                });
            },
            handleAdd: function (index,row) {
                this.addFormVisible = true;
                this.addForm = {
                    name: '',
                    parentId:'',
                    path: '',
                    component: '',
                    iconCls: '',
                    actionCode: '',
                    hidden:'0'
                }
                if(row){
                    this.addForm.parentId = row.id
                }
            },
            handleEdit: function(index,row){
                this.editFormVisible = true;
                this.editForm = {
                    id:row.id,
                    name: row.name,
                    parentId:row.parentId,
                    path: row.path,
                    component: row.component,
                    iconCls: row.icon,
                    actionCode: row.actionCode,
                    hidden:row.hidden
                }
            },
            selsChange: function (sels) {
                this.sels = sels;
            },
            //新增
            addSubmit: function () {
                let _this = this;
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.addForm);
                            if(para.parentId==undefined ||para.parentId==''){
                                para.parentId = 0;
                            }
                            // para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
                            saveMenu(para).then((res) => {
                                if(res.code == 200){
                                    this.addLoading = false;
                                    //NProgress.done();
                                    this.$message({
                                        message: '提交成功',
                                        type: 'success'
                                    });
                                    this.$refs['addForm'].resetFields();
                                    this.addFormVisible = false;
                                    this.getMenus();
                                }else{
                                    _this.$message({
                                        type: 'error',
                                        message: res.msg
                                    })
                                }
                            });
                        });
                    }
                });
            },
            editSubmit:function(){
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.editForm);
                            // para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
                            saveMenu(para).then((res) => {
                                if(res.code == 200){
                                    this.editLoading = false;
                                    //NProgress.done();
                                    this.$message({
                                        message: '编辑成功',
                                        type: 'success'
                                    });
                                    this.$refs['editForm'].resetFields();
                                    this.editFormVisible = false;
                                    this.getMenus();
                                }
                            });
                        });
                    }
                });
            }
        },
        mounted() {
            this.getMenus()
        }
    }
</script>

<style scoped>

</style>
